package com.foxsteps.gsonformat.utils;

import com.foxsteps.gsonformat.apiutils.PsiAnnotationUtils;
import com.foxsteps.gsonformat.enums.AnnotationClass;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierList;

/**
 * PsiElement操作工具类
 *
 * @author chen
 * @date 2023年04月09日 8:52
 */
public class PsiElementUtils {
    public static PsiElement checkReplaceJavaDoc(PsiElement psiElement) {
        try {
            if(!(psiElement instanceof PsiMethod)){
                return null;
            }
            PsiMethod psiMethod = (PsiMethod) psiElement;
            for(PsiElement subMethod : psiMethod.getChildren()){
                if(!(subMethod instanceof  PsiModifierList)){
                    continue;
                }
                PsiModifierList psiModifierList = (PsiModifierList) subMethod;
                for (PsiElement sub : psiModifierList.getChildren()) {
                    if(!(sub instanceof PsiAnnotation)){
                        continue;
                    }
                    PsiAnnotation subPsiAnnotation = (PsiAnnotation) sub;
                    boolean checkFlag = PsiAnnotationUtils.INSTANCE.isAssignableFrom(AnnotationClass.MASTRUCT_MAPPING.getClassName(), subPsiAnnotation);
                    if (checkFlag) {
                        return sub;
                    }
                }
            }
        }catch (Exception e){
            ;
        }
        return null;
    }
}
